<?php
/**
* @file $Id: Rollover.php 598 2007-07-12 22:50:24Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

$next_syear = UserSyear()+1;
$tables = array('USERS'=>_('Users'),'SCHOOL_PERIODS'=>_('School Periods'),'SCHOOL_YEARS'=>_('Marking Periods'),'ATTENDANCE_CALENDARS'=>_('Calendars'),'REPORT_CARD_GRADES'=>_('Report Card Grade Codes'),'COURSES'=>_('Courses').'<b>*</b>','STUDENT_ENROLLMENT'=>_('Students'),'REPORT_CARD_COMMENTS'=>_('Report Card Comment Codes'),'ELIGIBILITY_ACTIVITIES'=>_('Eligibility Activity Codes'),'ATTENDANCE_CODES'=>_('Attendance Codes'),'STUDENT_ENROLLMENT_CODES'=>_('Student Enrollment Codes'),'DISCIPLINE_FIELD_USAGE'=>_('Referral Form'));
$no_school_tables = array('STUDENT_ENROLLMENT_CODES'=>true,'USERS'=>true);

$table_list = '<TABLE align='.ALIGN_LEFT.'>';
foreach($tables as $table=>$name)
{
	$exists_RET[$table] = DBGet(DBQuery("SELECT count(*) AS COUNT from $table WHERE SYEAR='$next_syear'".(!$no_school_tables[$table]?" AND SCHOOL_ID='".UserSchool()."'":'')));
	if($exists_RET[$table][1]['COUNT']>0)
		$table_list .= '<TR><TD><INPUT type=checkbox value=Y name=tables['.$table.']></TD><TD><font color=grey>'.$name.' ('.$exists_RET[$table][1]['COUNT'].')</font></TD></TR>';
	else
		$table_list .= '<TR><TD><INPUT type=checkbox value=Y name=tables['.$table.'] CHECKED></TD><TD>'.$name.'</TD></TR>';
}
$table_list .= '</TABLE></CENTER><BR><small>* '._('You <i>must</i> roll users, school periods, marking periods, calendars, and report card grade codes at the same time or before rolling courses<BR><BR>Greyed items have already have data in the next school year (They might have been rolled).<BR><BR>Rolling greyed items will delete already existing data in the next school year.').'</small><CENTER>';

DrawHeader(ProgramTitle());

if(Prompt(_('Confirm Rollover'),_('Are you sure you want to roll the data for ').UserSyear().'-'.(UserSyear()+1)._(' to the next school year?'),$table_list))
{
	if($_REQUEST['tables']['COURSES'] && ((!$_REQUEST['tables']['USERS'] && $exists_RET['USERS'][1]['COUNT']<1) || (!$_REQUEST['tables']['SCHOOL_PERIODS'] && $exists_RET['SCHOOL_PERIODS'][1]['COUNT']<1) || (!$_REQUEST['tables']['REPORT_CARD_GRADES'] && $exists_RET['REPORT_CARD_GRADES'][1]['COUNT']<1) || (!$_REQUEST['tables']['SCHOOL_YEARS'] && $exists_RET['SCHOOL_YEARS'][1]['COUNT']<1)))
		BackPrompt(_('You must roll users, school periods, marking periods, and report card grade codes at the same time or before rolling courses.'));
	if(count($_REQUEST['tables']))
	{
		foreach($_REQUEST['tables'] as $table=>$value)
		{
			if($exists_RET[$table][1]['COUNT']>0)
				DBQuery("DELETE FROM $table WHERE SYEAR='".$next_syear."'".(!$no_school_tables[$table]?" AND SCHOOL_ID='".UserSchool()."'":''));
			Rollover($table);
		}
	}
	echo '<FORM action=Modules.php?modname='.$_REQUEST['modname'].' method=POST>';
	DrawHeader('<IMG SRC=assets/check.gif>'._('Your school\'s information has been rolled.'),'<INPUT type=submit value='._('OK').'>');
	echo '</FORM>';
	unset($_SESSION['_REQUEST_vars']['tables']);
	unset($_SESSION['_REQUEST_vars']['delete_ok']);
}

function Rollover($table)
{	global $next_syear;

	switch($table)
	{
		case 'USERS':
			$table = 'USERS';
			DBQuery("UPDATE USERS SET ROLLOVER_ID=STAFF_ID WHERE SYEAR='".UserSyear()."'");
			$table_properties = db_properties($table);
			foreach($table_properties as $column=>$values)
			{
				if($column!='STAFF_ID' && $column!='SYEAR' && substr($column,0,1)!='.')
					$columns .= ','.$column;
			}
			DBQuery("INSERT INTO $table (STAFF_ID,SYEAR".$columns.") SELECT nextval('".$table."_SEQ'),SYEAR+1".$columns." FROM $table WHERE SYEAR='".UserSyear()."'");
		break;

		case 'SCHOOL_PERIODS':
			DBQuery("UPDATE SCHOOL_PERIODS SET ROLLOVER_ID=PERIOD_ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			$table_properties = db_properties($table);
			foreach($table_properties as $column=>$values)
			{
				if($column!='PERIOD_ID' && $column!='SYEAR' && substr($column,0,1)!='.')
					$columns .= ','.$column;
			}
			DBQuery("INSERT INTO $table (PERIOD_ID,SYEAR".$columns.") SELECT nextval('".$table."_SEQ'),SYEAR+1".$columns." FROM $table WHERE SYEAR='".UserSyear()."'");
		break;

		case 'SCHOOL_YEARS':
			DBQuery("DELETE FROM SCHOOL_SEMESTERS WHERE SYEAR='".$next_syear."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("DELETE FROM SCHOOL_QUARTERS WHERE SYEAR='".$next_syear."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("DELETE FROM SCHOOL_PROGRESS_PERIODS WHERE SYEAR='".$next_syear."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("UPDATE SCHOOL_YEARS SET ROLLOVER_ID=MARKING_PERIOD_ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("INSERT INTO SCHOOL_YEARS (MARKING_PERIOD_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT nextval('MARKING_PERIOD_SEQ'),SYEAR+1,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_YEARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("UPDATE SCHOOL_SEMESTERS SET ROLLOVER_ID=MARKING_PERIOD_ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("INSERT INTO SCHOOL_SEMESTERS (MARKING_PERIOD_ID,YEAR_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT nextval('MARKING_PERIOD_SEQ'),(SELECT MARKING_PERIOD_ID FROM SCHOOL_YEARS y WHERE y.SYEAR=s.SYEAR+1 AND y.ROLLOVER_ID=s.YEAR_ID),SYEAR+1,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_SEMESTERS s WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("UPDATE SCHOOL_QUARTERS SET ROLLOVER_ID=MARKING_PERIOD_ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("INSERT INTO SCHOOL_QUARTERS (MARKING_PERIOD_ID,SEMESTER_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT nextval('MARKING_PERIOD_SEQ'),(SELECT MARKING_PERIOD_ID FROM SCHOOL_SEMESTERS s WHERE s.SYEAR=q.SYEAR+1 AND s.ROLLOVER_ID=q.SEMESTER_ID),SYEAR+1,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_QUARTERS q WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("UPDATE SCHOOL_PROGRESS_PERIODS SET ROLLOVER_ID=MARKING_PERIOD_ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("INSERT INTO SCHOOL_PROGRESS_PERIODS (MARKING_PERIOD_ID,QUARTER_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT nextval('MARKING_PERIOD_SEQ'),(SELECT MARKING_PERIOD_ID FROM SCHOOL_QUARTERS q WHERE q.SYEAR=p.SYEAR+1 AND q.ROLLOVER_ID=p.QUARTER_ID),SYEAR+1,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_PROGRESS_PERIODS p WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
		break;

		case 'ATTENDANCE_CALENDARS':
			DBQuery("UPDATE $table SET ROLLOVER_ID=CALENDAR_ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			$table_properties = db_properties($table);
			$columns = '';
			foreach($table_properties as $column=>$values)
			{
				if($column!='CALENDAR_ID' && $column!='SYEAR')
					$columns .= ','.$column;
			}
			DBQuery("INSERT INTO $table (CALENDAR_ID,SYEAR".$columns.") SELECT nextval('CALENDARS_SEQ'),SYEAR+1".$columns." FROM $table WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			// IF THERE ARE ALREADY STUDENTS ENROLLED IN THIS SCHOOL (perhaps graduated from a different school through Rollover), set their calendar_id
			
			$calendar_id = DBGet(DBQuery("SELECT CALENDAR_ID FROM ATTENDANCE_CALENDARS WHERE SYEAR='$next_syear' AND SCHOOL_ID='".UserSchool()."' ORDER BY DEFAULT_CALENDAR"));
			$calendar_id = $calendar_id[1]['CALENDAR_ID'];
			if($calendar_id)
				DBQuery("UPDATE STUDENT_ENROLLMENT SET CALENDAR_ID='$calendar_id' WHERE CALENDAR_ID IS NULL AND SYEAR='$next_syear' AND SCHOOL_ID='".UserSchool()."'");
		break;

		case 'COURSES':
			DBQuery("DELETE FROM COURSE_SUBJECTS WHERE SYEAR='".$next_syear."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("DELETE FROM COURSE_WEIGHTS WHERE SYEAR='".$next_syear."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("DELETE FROM COURSE_PERIODS WHERE SYEAR='".$next_syear."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("UPDATE COURSE_SUBJECTS SET ROLLOVER_ID=SUBJECT_ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("INSERT INTO COURSE_SUBJECTS (SYEAR,SCHOOL_ID,SUBJECT_ID,TITLE,SHORT_NAME,ROLLOVER_ID) SELECT SYEAR+1,SCHOOL_ID,nextval('COURSE_SUBJECTS_SEQ'),TITLE,SHORT_NAME,ROLLOVER_ID FROM COURSE_SUBJECTS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("UPDATE COURSES SET ROLLOVER_ID=COURSE_ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("INSERT INTO COURSES (SYEAR,COURSE_ID,SUBJECT_ID,SCHOOL_ID,GRADE_LEVEL,TITLE,SHORT_NAME,GRAD_SUBJECT_ID,PREREQUISITES,ROLLOVER_ID) SELECT SYEAR+1 as SYEAR,nextval('COURSES_SEQ'),(SELECT SUBJECT_ID FROM COURSE_SUBJECTS s WHERE s.SYEAR=c.SYEAR+1 AND s.ROLLOVER_ID=c.SUBJECT_ID) AS SUBJECT_ID,SCHOOL_ID,GRADE_LEVEL,TITLE,SHORT_NAME,GRAD_SUBJECT_ID,PREREQUISITES,ROLLOVER_ID FROM COURSES c WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("INSERT INTO COURSE_WEIGHTS (SYEAR,SCHOOL_ID,COURSE_ID,GPA_MULTIPLIER,COURSE_WEIGHT,CREDITS) SELECT SYEAR+1,SCHOOL_ID,(SELECT COURSE_ID FROM COURSES c WHERE c.SYEAR=w.SYEAR+1 AND c.ROLLOVER_ID=w.COURSE_ID),GPA_MULTIPLIER,COURSE_WEIGHT,CREDITS FROM COURSE_WEIGHTS w WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("UPDATE COURSE_PERIODS SET ROLLOVER_ID=COURSE_PERIOD_ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("INSERT INTO COURSE_PERIODS (SYEAR,SCHOOL_ID,COURSE_PERIOD_ID,COURSE_ID,COURSE_WEIGHT,TITLE,SHORT_NAME,PERIOD_ID,MP,MARKING_PERIOD_ID,TEACHER_ID,ROOM,TOTAL_SEATS,FILLED_SEATS,DOES_ATTENDANCE,DOES_GRADES,DOES_GPA,GENDER_RESTRICTION,HOUSE_RESTRICTION,CREDITS,AVAILABILITY,ROLLOVER_ID,DAYS,GRADE_SCALE_ID,CALENDAR_ID) 
				SELECT SYEAR+1,SCHOOL_ID,nextval('COURSE_PERIODS_SEQ'),
				(SELECT COURSE_ID FROM COURSES c WHERE c.SYEAR=p.SYEAR+1 AND c.ROLLOVER_ID=p.COURSE_ID),
				COURSE_WEIGHT,TITLE,SHORT_NAME,
				(SELECT PERIOD_ID FROM SCHOOL_PERIODS n WHERE n.SYEAR=p.SYEAR+1 AND n.ROLLOVER_ID=p.PERIOD_ID),MP,
				".db_case(array('MP',"'FY'","'0'","'SEM'",'(SELECT MARKING_PERIOD_ID FROM SCHOOL_SEMESTERS n WHERE n.SYEAR=p.SYEAR+1 AND n.ROLLOVER_ID=p.MARKING_PERIOD_ID)',"'QTR'",'(SELECT MARKING_PERIOD_ID FROM SCHOOL_QUARTERS n WHERE n.SYEAR=p.SYEAR+1 AND n.ROLLOVER_ID=p.MARKING_PERIOD_ID)')).",
				(SELECT STAFF_ID FROM USERS u WHERE u.SYEAR=p.SYEAR+1 AND u.ROLLOVER_ID=p.TEACHER_ID),
				ROOM,TOTAL_SEATS,0 AS FILLED_SEATS,DOES_ATTENDANCE,DOES_GRADES,DOES_GPA,GENDER_RESTRICTION,HOUSE_RESTRICTION,CREDITS,AVAILABILITY,ROLLOVER_ID,DAYS,
				(SELECT ID FROM REPORT_CARD_GRADE_SCALES s WHERE s.SYEAR=p.SYEAR+1 AND s.ROLLOVER_ID=p.GRADE_SCALE_ID), 
				(SELECT CALENDAR_ID FROM ATTENDANCE_CALENDARS ac WHERE ac.SYEAR=p.SYEAR+1 AND ac.ROLLOVER_ID=p.CALENDAR_ID) 
					FROM COURSE_PERIODS p WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			// parent id, calendar_id

			// UPDATE THE PARENT ID's - if there were sections joined to other sections last year, we'll have to reset the associations after rollover.
			DBQuery("UPDATE COURSE_PERIODS SET PARENT_ID=COURSE_PERIOD_ID WHERE SYEAR='".$next_syear."' AND SCHOOL_ID='".UserSchool()."'");
		break;

		case 'STUDENT_ENROLLMENT':
			// ROLL STUDENTS TO NEXT GRADE
			DBQuery("INSERT INTO STUDENT_ENROLLMENT (ID,SYEAR,SCHOOL_ID,STUDENT_ID,GRADE_ID,START_DATE,END_DATE,ENROLLMENT_CODE,DROP_CODE,CALENDAR_ID,INCLUDE_IN_CLASS_RANK) SELECT nextval('STUDENT_ENROLLMENT_SEQ'),SYEAR+1,SCHOOL_ID,STUDENT_ID,(SELECT NEXT_GRADE_ID FROM SCHOOL_GRADELEVELS g WHERE g.ID=e.GRADE_ID),START_DATE+365 AS START_DATE,NULL AS END_DATE,ENROLLMENT_CODE,NULL AS DROP_CODE,(SELECT CALENDAR_ID FROM ATTENDANCE_CALENDARS ac WHERE ac.SYEAR=e.SYEAR+1 AND ac.ROLLOVER_ID=e.CALENDAR_ID),INCLUDE_IN_CLASS_RANK FROM STUDENT_ENROLLMENT e WHERE e.SYEAR='".UserSyear()."' AND e.SCHOOL_ID='".UserSchool()."' AND (('".DBDate()."' BETWEEN e.START_DATE AND e.END_DATE OR e.END_DATE IS NULL) AND '".DBDate()."'>=e.START_DATE) AND e.NEXT_SCHOOL='".UserSchool()."'");

			// ROLL STUDENTS WHO ARE TO BE RETAINED
			DBQuery("INSERT INTO STUDENT_ENROLLMENT (ID,SYEAR,SCHOOL_ID,STUDENT_ID,GRADE_ID,START_DATE,END_DATE,ENROLLMENT_CODE,DROP_CODE,CALENDAR_ID,INCLUDE_IN_CLASS_RANK) SELECT nextval('STUDENT_ENROLLMENT_SEQ'),SYEAR+1,SCHOOL_ID,STUDENT_ID,GRADE_ID,START_DATE+365 AS START_DATE,NULL AS END_DATE,ENROLLMENT_CODE,NULL AS DROP_CODE,(SELECT CALENDAR_ID FROM ATTENDANCE_CALENDARS ac WHERE ac.SYEAR=e.SYEAR+1 AND ac.ROLLOVER_ID=e.CALENDAR_ID),INCLUDE_IN_CLASS_RANK FROM STUDENT_ENROLLMENT e WHERE e.SYEAR='".UserSyear()."' AND e.SCHOOL_ID='".UserSchool()."' AND (('".DBDate()."' BETWEEN e.START_DATE AND e.END_DATE OR e.END_DATE IS NULL) AND '".DBDate()."'>=e.START_DATE) AND e.NEXT_SCHOOL='0'");

			// ROLL STUDENTS TO NEXT SCHOOL
			//DBQuery("DELETE FROM STUDENT_ENROLLMENT WHERE SYEAR='".$next_syear."' AND EXISTS (SELECT '' FROM STUDENT_ENROLLMENT e WHERE e.STUDENT_ID=STUDENT_ENROLLMENT.STUDENT_ID AND e.SYEAR='".UserSyear()."' AND e.SCHOOL_ID='".UserSchool()."' AND (('".DBDate()."' BETWEEN e.START_DATE AND e.END_DATE OR e.END_DATE IS NULL) AND '".DBDate()."'>=e.START_DATE) AND e.NEXT_SCHOOL NOT IN ('".UserSchool()."','0','-1'))");
			// THIS WON't WORK IN ORACLE BECAUSE OF THE LIMIT 1 STATEMENT
			DBQuery("INSERT INTO STUDENT_ENROLLMENT (ID,SYEAR,SCHOOL_ID,STUDENT_ID,GRADE_ID,START_DATE,END_DATE,ENROLLMENT_CODE,DROP_CODE,CALENDAR_ID,INCLUDE_IN_CLASS_RANK) SELECT nextval('STUDENT_ENROLLMENT_SEQ'),SYEAR+1,NEXT_SCHOOL,STUDENT_ID,(SELECT g.ID FROM SCHOOL_GRADELEVELS g WHERE g.SORT_ORDER=1 AND g.SCHOOL_ID=e.NEXT_SCHOOL),START_DATE+365 AS START_DATE,NULL AS END_DATE,ENROLLMENT_CODE,NULL AS DROP_CODE,(SELECT CALENDAR_ID FROM ATTENDANCE_CALENDARS ac WHERE ac.SYEAR=e.SYEAR+1 AND ac.SCHOOL_ID=e.NEXT_SCHOOL ORDER BY DEFAULT_CALENDAR LIMIT 1),INCLUDE_IN_CLASS_RANK FROM STUDENT_ENROLLMENT e WHERE e.SYEAR='".UserSyear()."' AND e.NEXT_SCHOOL='".UserSchool()."' AND e.SCHOOL_ID!='".UserSchool()."' AND (('".DBDate()."' BETWEEN e.START_DATE AND e.END_DATE OR e.END_DATE IS NULL) AND '".DBDate()."'>=e.START_DATE)");
		break;

		case 'REPORT_CARD_GRADES':
			DBQuery("DELETE FROM REPORT_CARD_GRADE_SCALES WHERE SYEAR='".$next_syear."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("UPDATE REPORT_CARD_GRADE_SCALES SET ROLLOVER_ID=ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			$table_properties = db_properties('REPORT_CARD_GRADE_SCALES');
			$columns = '';
			foreach($table_properties as $column=>$values)
			{
				if($column!='ID' && $column!='SYEAR')
					$columns .= ','.$column;
			}
			DBQuery("INSERT INTO REPORT_CARD_GRADE_SCALES (ID,SYEAR".$columns.") SELECT nextval('REPORT_CARD_GRADE_SCALES_SEQ'),SYEAR+1".$columns." FROM REPORT_CARD_GRADE_SCALES WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			$table_properties = db_properties($table);
			$columns = '';
			foreach($table_properties as $column=>$values)
			{
				if($column!='ID' && $column!='SYEAR' && $column!='SCALE_ID')
					$columns .= ','.$column;
			}
			DBQuery("INSERT INTO $table (ID,SYEAR,SCALE_ID".$columns.") SELECT nextval('".$table."_SEQ'),SYEAR+1,(SELECT ID FROM REPORT_CARD_GRADE_SCALES s WHERE s.SYEAR=g.SYEAR+1 AND s.ROLLOVER_ID=g.SCALE_ID)".$columns." FROM REPORT_CARD_GRADES g WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
		break;

		case 'ATTENDANCE_CODES':
			DBQuery("DELETE FROM ATTENDANCE_CODE_CATEGORIES WHERE SYEAR='".$next_syear."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("UPDATE ATTENDANCE_CODE_CATEGORIES SET ROLLOVER_ID=ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			$table_properties = db_properties('ATTENDANCE_CODE_CATEGORIES');
			$columns = '';
			foreach($table_properties as $column=>$values)
			{
				if($column!='ID' && $column!='SYEAR')
					$columns .= ','.$column;
			}
			DBQuery("INSERT INTO ATTENDANCE_CODE_CATEGORIES (ID,SYEAR".$columns.") SELECT nextval('ATTENDANCE_CODE_CATEGORIES_SEQ'),SYEAR+1".$columns." FROM ATTENDANCE_CODE_CATEGORIES WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("DELETE FROM ATTENDANCE_CODES WHERE SYEAR='".$next_syear."' AND SCHOOL_ID='".UserSchool()."'");
			$table_properties = db_properties($table);
			$columns = '';
			foreach($table_properties as $column=>$values)
			{
				if($column!='ID' && $column!='SYEAR' && $column!='TABLE_NAME')
					$columns .= ','.$column;
			}
			DBQuery("INSERT INTO $table (ID,SYEAR,TABLE_NAME".$columns.") SELECT nextval('".$table."_SEQ'),SYEAR+1,(SELECT ID FROM ATTENDANCE_CODE_CATEGORIES s WHERE s.SYEAR=c.SYEAR+1 AND s.ROLLOVER_ID=c.TABLE_NAME)".$columns." FROM ATTENDANCE_CODES c WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
		break;

		case 'REPORT_CARD_COMMENTS':
		case 'ELIGIBILITY_ACTIVITIES':
		case 'DISCIPLINE_FIELD_USAGE':
			$table_properties = db_properties($table);
			$columns = '';
			foreach($table_properties as $column=>$values)
			{
				if($column!='ID' && $column!='SYEAR')
					$columns .= ','.$column;
			}
			DBQuery("INSERT INTO $table (ID,SYEAR".$columns.") SELECT nextval('".$table."_SEQ'),SYEAR+1".$columns." FROM $table WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
		break;

		// DOESN'T HAVE A SCHOOL_ID
		case 'STUDENT_ENROLLMENT_CODES':
			$table_properties = db_properties($table);
			$columns = '';
			foreach($table_properties as $column=>$values)
			{
				if($column!='ID' && $column!='SYEAR')
					$columns .= ','.$column;
			}
			DBQuery("INSERT INTO $table (ID,SYEAR".$columns.") SELECT nextval('".$table."_SEQ'),SYEAR+1".$columns." FROM $table WHERE SYEAR='".UserSyear()."'");
		break;
	}
}
?>